#!/bin/bash

# $@ 服务名 多个以空格分隔 如large backend
# 服务目录名 如xyz/bbc
# 部署前提 1 -> 已有nginx 2 -> web包目录放到指定目录ymd文件夹下 3 -> web压缩包直接压缩的是dist文件夹下面的文件

# 获取服务目录名
function get_directory() {
  case "$1" in
  "backend")
  echo ""
  ;;
  "large")
  echo "large"
  ;;
  *)
  echo 404
  ;;
  esac
}

echo deploy $*

# 目录处理
date_str=$(date +%Y%m%d%H%M%S)
source_directory="/usr/local/packages/web"
target_directory="/usr/local/services/nginx/html/dist"

# 循环处理
for s in "$@"
do
  f_name=$(get_directory "$s")
  if [ "$f_name" != "404" ]
  then
    echo "deploy service $s"
    source_zip="${source_directory}/dist_${s}.zip"
    rename_zip="${source_directory}/dist_${s}_${date_str}.zip"
    unzip_directory="${target_directory}/${f_name}/"
    unzip -o -qq -d "$unzip_directory" "$source_zip"
    mv "$source_zip" "$rename_zip"
    echo "deploy $s success"
  fi
done